//在地图上显示情报(防御指数，通行指数)

var warInfo = {
	defendSprites : [],
	passSprites : [],
	
	_warInfoType : 0,
	
	_warInfoType_def : {
		none : 0,
		defend : 1,
		pass : 2,
		defend_and_pass : 3
	},
	
	Initialize : function(){
		this.defendSprites = [];
		this.passSprites = [];
		this._warInfoType = 0;
	},
	
	_ShowTileDefend : function(tileIndex, defend_index){
		if (this.defendSprites[tileIndex] != null){
			this.defendSprites[tileIndex].removeFromParent();
			this.defendSprites[tileIndex] = null;
		}

		var newSprite = null;
		if (defend_index < 10){
			//nothing
		}else if (defend_index < 20){
			newSprite = new cc.Sprite(resource.defendLevel1_png);
		}else if (defend_index < 40){
			newSprite = new cc.Sprite(resource.defendLevel2_png);
		}else{
			newSprite = new cc.Sprite(resource.defendLevel3_png);
		}
		if (newSprite != null){
			newSprite.attr(tileEngine.IndexToCoord(tileIndex));
			mainMap.node.addChild(newSprite, 4);
			this.defendSprites[tileIndex] = newSprite;
		}
	},
	
	_HideTileDefend : function(tileIndex){
		if (this.defendSprites[tileIndex] != null){
			this.defendSprites[tileIndex].removeFromParent();
			this.defendSprites[tileIndex] = null;
		}
	},
	
	_ShowTilePass : function(tileIndex, pass_index){
		if (this.passSprites[tileIndex] != null){
			this.passSprites[tileIndex].removeFromParent();
			this.passSprites[tileIndex] = null;
		}

		var newSprite = null;
		if (pass_index >= 40){
			//nothing
		}else if (pass_index >= 20){
			newSprite = new cc.Sprite(resource.passLevel1_png);
		}else if (pass_index >= 10){
			newSprite = new cc.Sprite(resource.passLevel2_png);
		}else{
			newSprite = new cc.Sprite(resource.passLevel3_png);
		}
		if (newSprite != null){
			newSprite.attr(tileEngine.IndexToCoord(tileIndex));
			mainMap.node.addChild(newSprite, 4);
			this.passSprites[tileIndex] = newSprite;
		}
	},
	
	_HideTilePass : function(tileIndex){
		if (this.passSprites[tileIndex] != null){
			this.passSprites[tileIndex].removeFromParent();
			this.passSprites[tileIndex] = null;
		}
	},
	
	ShowUI : function(){
		var forceid = showingForce;
		if (this._warInfoType % 2 >= 1){
			var defend_index = forceInfo[forceid].defend_index;
			for (var tileIndex = 0; tileIndex < tileEngine.tileCount; tileIndex++){
				this._ShowTileDefend(tileIndex, defend_index[tileIndex]);
			}
		}
		if (this._warInfoType / 2 >= 1){
			var pass_index = forceInfo[forceid].pass_index;
			for (var tileIndex = 0; tileIndex < tileEngine.tileCount; tileIndex++){
				this._ShowTilePass(tileIndex, pass_index[tileIndex]);
			}
		}
	},
	
	HideUI : function(){
		for (var tileIndex = 0; tileIndex < tileEngine.tileCount; tileIndex++){
			this._HideTileDefend(tileIndex);
		}
		for (var tileIndex = 0; tileIndex < tileEngine.tileCount; tileIndex++){
			this._HideTilePass(tileIndex);
		}
	},
	
	SwitchUI : function(){
		var newWarInfoType = (this._warInfoType + 1) % 4;
		var forceid = showingForce;
		if (newWarInfoType % 2 >= 1 && this._warInfoType % 2 < 1){
			var defend_index = forceInfo[forceid].defend_index;
			for (var tileIndex = 0; tileIndex < tileEngine.tileCount; tileIndex++){
				this._ShowTileDefend(tileIndex, defend_index[tileIndex]);
			}
		}
		if (newWarInfoType % 2 < 1 && this._warInfoType % 2 >= 1){
			for (var tileIndex = 0; tileIndex < tileEngine.tileCount; tileIndex++){
				this._HideTileDefend(tileIndex);
			}
		}
		if (newWarInfoType / 2 >= 1 && this._warInfoType / 2 < 1){
			var pass_index = forceInfo[forceid].pass_index;
			for (var tileIndex = 0; tileIndex < tileEngine.tileCount; tileIndex++){
				this._ShowTilePass(tileIndex, pass_index[tileIndex]);
			}
		}
		if (newWarInfoType / 2 < 1 && this._warInfoType / 2 >= 1){
			for (var tileIndex = 0; tileIndex < tileEngine.tileCount; tileIndex++){
				this._HideTilePass(tileIndex);
			}
		}
		this._warInfoType = newWarInfoType;
	},
	
	//刷新势力情报，用于部队被攻击动画
	RefreshTileInfo : function(tileIndex){
		var forceid = showingForce;
		if (this._warInfoType % 2 >= 1){
			var defend_index = forceInfo[forceid].defend_index;
			this._ShowTileDefend(tileIndex, defend_index[tileIndex]);
		}
		if (this._warInfoType / 2 >= 1){
			var pass_index = forceInfo[forceid].pass_index;
			this._ShowTilePass(tileIndex, pass_index[tileIndex]);
		}
	},
	
	//刷新势力情报，用于部队移动动画
	RefreshTileAndNeighbourInfo : function(tileIndex){
		var forceid = showingForce;
		if (this._warInfoType % 2 >= 1){
			var defend_index = forceInfo[forceid].defend_index;
			this._ShowTileDefend(tileIndex, defend_index[tileIndex]);
			for (var neighborid in tileEngine.neighborCache[tileIndex]){
				var tgtindex = tileEngine.neighborCache[tileIndex][neighborid];
				this._ShowTileDefend(tgtindex, defend_index[tgtindex]);
			}
		}
		if (this._warInfoType / 2 >= 1){
			var pass_index = forceInfo[forceid].pass_index;
			this._ShowTilePass(tileIndex, pass_index[tileIndex]);
			for (var neighborid in tileEngine.neighborCache[tileIndex]){
				var tgtindex = tileEngine.neighborCache[tileIndex][neighborid];
				this._ShowTilePass(tgtindex, pass_index[tgtindex]);
			}
		}
	}
};

control.onWarInfoClick = function(sender){
	warInfo.SwitchUI();
};